package com.pms.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class StringUtility {
	/** Returns a String based on the supplied String but with any instances
	 * of the specified character removed.
	 *
	 * @param String   the String the substrings are replaced in
	 * @param char     the character to remove
	 *
	 * @return String   the resulting String with character removed
	 */
	public static String remove(String aStringToFilter, char aCharToRemove) {
		
		if(aStringToFilter.indexOf(aCharToRemove) < 0) {
			return aStringToFilter;
		}
		
		StringBuffer resultStringBuffer = new StringBuffer();
		char[] charArray = aStringToFilter.toCharArray();
		for(int index = 0; index < charArray.length; index++) {
			char ch = charArray[index];
			if(ch != aCharToRemove) {
				resultStringBuffer.append(ch);
			}
		}
		
		return resultStringBuffer.toString();
	}
	
	/** Replaces a substring within a String with another substring.
	 *
	 * @param String   the String the substrings are replaced in
	 * @param String   the sub string to be replaced
	 * @param String   the sub string to replace with
	 *
	 * @return String   the resulting String with substrings replaced
	 */
	public static String replace(String aStringToReplaceIn, String aSubStringToReplace, String aSubStringToReplaceWith) {
		
		if(aStringToReplaceIn == null)
			return null;
		
		StringBuffer resultString = new StringBuffer();
		int lengthOfsubStringToReplace = aSubStringToReplace.length();
		int currentOffset = 0;
		int index;
		
		// replace every occurance
		while(true) {
			index = aStringToReplaceIn.indexOf(aSubStringToReplace, currentOffset);
			if(index == -1) {
				resultString.append(aStringToReplaceIn.substring(currentOffset, aStringToReplaceIn.length()));
				return resultString.toString();
			}
			resultString.append(aStringToReplaceIn.substring(currentOffset, index));
			resultString.append(aSubStringToReplaceWith);
			currentOffset = index + lengthOfsubStringToReplace;
		}
	}
	
	
	/** Trims a string to the specified size.
	 *
	 * @param String  String to be trimmed
	 * @param int     Maximum Size
	 *
	 * @return String Trimmed String
	 */
	public static String trimString(String aStringToTrim, int aMaxSize) {
		
		return (aStringToTrim.length() > aMaxSize ? aStringToTrim.substring(0, aMaxSize) : aStringToTrim);
	}
	
	/** Returns true if the String contains any white space characters.
	 *
	 * @param String  String to check
	 *
	 * @return boolean
	 */
	public static boolean containsWhiteSpace(String aString) {
		
		boolean result = false;
		
		if( (aString.indexOf(' ') != -1) ||
				(aString.indexOf('\t') != -1) ||
				(aString.indexOf('\n') != -1) ) {
			result = true;
		}
		
		return result;
	}
	
	
	/** Takes an input String and tokenizes into words using a default
	 * StringTokenizer which breaks on white space.
	 *
	 * @param String   a string of words
	 *
	 * @return ArrayList of Strings
	 */
	
	public static ArrayList tokenizeToArrayList(String aString) {
		
		return tokenizeToArrayList(new StringTokenizer(aString));
	}
	
	/** Takes an input String and delimiter and tokenizes the string into words using the delimiter
	 *
	 * @param String   a string of words
	 * @param String   delimiter
	 *
	 * @return ArrayList of Strings
	 */
	
	public static ArrayList tokenizeToArrayList(String aString, String aDelim) {
		
		return tokenizeToArrayList(new StringTokenizer(aString, aDelim));
	}
	
	/** Takes a predefined StringTokenizer and tokenizes into an ArrayList of Strings.
	 *
	 * @param StringTokenizer
	 *
	 * @return ArrayList of Strings
	 */
	
	public static ArrayList tokenizeToArrayList(StringTokenizer aStringTokenizer) {
		
		ArrayList arrayList = new ArrayList();
		
		while(aStringTokenizer.hasMoreTokens()) {
			String string = aStringTokenizer.nextToken();
			arrayList.add(string);
		}
		
		return arrayList;
	}
	
	/**
	 * This method takes a String as an argument, checks for all the non alphanumeric
	 * characters from the String using regular expressions execpt "_", then checks
	 * for Words, from an EnumList, using pattern matching.
	 *
	 * If a Word is found it returns true
	 *
	 * @param String - the String which is checked for the Words.
	 * @param EnumList - An Enum List which maintains the list of Words.
	 * @return boolean - true or false.
	 */
	
	public static boolean findNonAlphaNumericStringExceptUnderscore(String aStringToSearch) {
		boolean nonAlphaNumeric = false ;
		
		// pattern searches for any non alphanumeric character except '_'
		String aNonAlphaNumericPattern = "#"+"[^a-zA-Z0-9_]"+"#";
		
		nonAlphaNumeric=searchString(aNonAlphaNumericPattern,aStringToSearch);
		return(nonAlphaNumeric);
	}
	
	
	/** Searches a String with a Pattern passed as an input to the method.
	 *
	 * @param String  String to be searched
	 * @param String  Pattern to be searched
	 * @return True if found
	 */
	
	public static boolean searchString(String aPattern, String aString){
		
		return aPattern.matches(aString);
	}
	
	public static String[] sortLexically(String[] aStrings) {
//		sorting by length
		Arrays.sort(aStrings, new Comparator()
				{ 
			public int compare(Object o1, Object o2)
			{
				if(((String) o1).length() < ((String) o2).length()) {
					return -1;
				}
				
				if(((String) o1).length() > ((String) o2).length()) {
					return 1;
				}
				
				int retVal=0;
				
				char c1[] = ((String) o1).toCharArray();
				char c2[] = ((String) o2).toCharArray();
				
				for(int i=0; i<c1.length;i++) {
					if(c1[i] > c2[i]) {
						return 1;                    		
					}
					if(c1[i] < c2[i]) {
						return -1;                    		
					}
				}
				return retVal;
			}
				});
		return aStrings;
	}
	
	public static String formatString(String data,String token,String replaceToken){
		StringTokenizer st = new StringTokenizer(data,token);
		StringBuffer sb = new StringBuffer();
		boolean flag = false;
		while(st.hasMoreTokens()){
			if(flag){
				sb.append(replaceToken+st.nextToken());
			}else{
				sb.append(st.nextToken());
			}
			flag=true;
		}
		return sb.toString();
	}
}
